package com.util;

import java.util.List;

public class Page {
	/** imply if the page has previous page */
	public boolean hasPrePage;
	/** 是否有下一页 */
	public boolean hasNextPage;
	/** 每页数量 */
	public int everyPage = 10;
	/** 当前页 */
	public int totalPage;
	/** 当前页 */
	public int currentPage = 1;
	/** 开始条数*/
	public int beginIndex;
	/**总行数 */
	public int totalRecords;
	// 分页效果，加前置url
	public String preUrl;
	// 分页效果，加后置url
	public String appendUrl;
	//分页效果 中间显示数量
	private int midNum = 5;
	// 前台展示所用html
	private String innerHtml;
	
	private List list ;//取回的list
	private Object otherObj;//取回的对象
	private Object otherObj2;//取回的对象2
	
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	public Object getOtherObj() {
		return otherObj;
	}
	public void setOtherObj(Object otherObj) {
		this.otherObj = otherObj;
	}
	public Object getOtherObj2() {
		return otherObj2;
	}
	public void setOtherObj2(Object otherObj2) {
		this.otherObj2 = otherObj2;
	}
	public int getTotalRecords() {
		return totalRecords;
	}
	public void setTotalRecords(int totalRecords) {
		this.totalRecords = totalRecords;
	}

	public String getPreUrl() {
		return preUrl == null ? "" : preUrl;
	}

	public void setPreUrl(String preUrl) {
		this.preUrl = preUrl;
	}

	public String getAppendUrl() {
		return appendUrl;
	}

	public void setAppendUrl(String appendUrl) {
		this.appendUrl = appendUrl;
	}

	public int getMidNum() {
		return midNum;
	}

	public void setMidNum(int midNum) {
		this.midNum = midNum;
	}

	// 前台请求分页代码时，将getNew08Nav2计算后的字符串返回
	public String getInnerHtml() {
		this.innerHtml = getNew08Nav2();
		return innerHtml;
	}

	public void setInnerHtml(String innerHtml) {
		this.innerHtml = innerHtml;
	}

	public Page() {

	}

	public Page(int everyPage) {
		this.everyPage = everyPage;
	}

	public Page(boolean hasPrePage, boolean hasNextPage, int everyPage, int totalPage, int currentPage, int beginIndex, int totalRecords) {
		this.hasPrePage = hasPrePage;
		this.hasNextPage = hasNextPage;
		this.everyPage = everyPage;
		this.totalPage = totalPage;
		this.currentPage = currentPage;
		this.beginIndex = beginIndex;
		this.totalRecords = totalRecords;
	}

	public int getBeginIndex() {
		return beginIndex;
	}

	public void setBeginIndex(int beginIndex) {
		this.beginIndex = beginIndex;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getEveryPage() {
		return everyPage;
	}

	public void setEveryPage(int everyPage) {
		this.everyPage = everyPage;
	}

	public boolean getHasNextPage() {
		return hasNextPage;
	}

	public void setHasNextPage(boolean hasNextPage) {
		this.hasNextPage = hasNextPage;
	}

	public boolean getHasPrePage() {
		return hasPrePage;
	}

	public void setHasPrePage(boolean hasPrePage) {
		this.hasPrePage = hasPrePage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	/**
	 *分页代码
	 * @return
	 */
	public String getNew08Nav2() {
		StringBuffer sbNav = new StringBuffer();
		int mid = 5;
		int midHalfNum = this.getMidNum() / 2;
		int now = this.getCurrentPage();
		int total = this.getTotalPage();
		sbNav.append("<span  class=\"disabled\">共:<strong>").append(total).append("</strong>页</span>");
		if (total > 1) {// 多页 需要分页
			// if (this.getHasNextPage()) {
			if (now == 1)
				sbNav.append("");
			if (now > 1)
				sbNav.append("<a href='").append(this.getPreUrl() + (now - 1) + this.getAppendUrl()).append("'><上一页</a>");
			// }

			if ((now + midHalfNum) >= total) {
				mid = total - midHalfNum - 1;
			} else {
				mid = now;
			}
			if (now != 1) {
				sbNav.append("<a href='").append(this.getPreUrl() + 1 + this.getAppendUrl()).append("'>").append(1).append("</a>");
			} else {
				sbNav.append("<span class=current>1</span>");
			}
			if ((mid - midHalfNum) > 2) {
				sbNav.append("...");
			}
			// for (int i = mid - midHalfNum; i <= mid + midHalfNum; i++) {
			int temp = 0;
			if (total > this.getMidNum()) {
				if ((mid + midHalfNum) < this.getMidNum()) {
					temp = this.getMidNum();
				} else {
					temp = mid + midHalfNum;
				}
			} else {
				temp = mid + midHalfNum;
			}
			for (int i = mid - midHalfNum; i <= temp; i++) {
				if (i > 1) {
					if (i == now) {
						sbNav.append("<span class=current>").append(i).append("</span>");
					} else {
						sbNav.append("<a href='").append(this.getPreUrl() + i + this.getAppendUrl()).append("'>").append(i).append("</a>");
					}
				}
			}
			if ((mid + midHalfNum + 1) < total) {
				sbNav.append("...");
			}
			if (now != total) {
				sbNav.append("<a href='").append(this.getPreUrl() + total + this.getAppendUrl()).append("'>").append(total).append("</a>");
			} else {
				sbNav.append("<span class=current>").append(total).append("</span>");
			}

			if (this.getHasNextPage()) {
				sbNav.append("<a href=\"").append(this.getPreUrl() + (this.getCurrentPage() + 1) + this.getAppendUrl()).append(
																																"\">下一页></a>");
			}
		}
		if (total > 1) {
			double id = Math.random();
			sbNav.append("第<input id=\"t" + id + "\" onkeyup=\"javascript:if(event.keyCode==13){if(this.value==''){return};document.getElementById('o'+" + id + ").click();};\" class=\"inp\" type=\"text\" size=\"5\" name=\"textfield\"/>页");
			sbNav.append("<input class=\"but\" id=\"o" + id + "\"  onclick=\"var tt=document.getElementById('t'+" + id + ");var num=tt.value;num=parseInt(num);if(isNaN(num)||(num>" + total + ")||(num<1)){alert('请输入正确的页码,并且不超出页码最大数！');tt.value='';}else{window.location.href=('" + this.getPreUrl() + "'+num+'" + this.getAppendUrl() + "');}\"  type=\"button\" value=\"GO\"/>");
		}
		return sbNav.toString();
	}
}
